package com.lhh.company.website.admin;

import com.lhh.company.website.config.SysUserDetails;
import com.lhh.company.website.entity.Res;
import com.lhh.company.website.entity.SysDict;
import com.lhh.company.website.entity.SysException;
import com.lhh.company.website.service.SysDictService;
import com.lhh.company.website.utils.SysUtils;
import jakarta.annotation.Resource;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Map;

@RestController
public class SysDictApi {

    @Resource
    private SysDictService sysDictService;

    @RequestMapping("/dict/get")
    public Res<ArrayList<SysDict>> getDict() {
        return Res.succ(SysUtils.sysDictList);
    }

    @RequestMapping("/dict/delete")
    public Res<ArrayList<SysDict>> deleteDict(@RequestBody Map<String, String> map) throws SysException {
        sysDictService.deleteDict(map);
        return Res.succ(SysUtils.sysDictList);
    }

    @RequestMapping("/dict/updateOrSave")
    public Res<ArrayList<SysDict>> updateOrSaveDict(@RequestBody SysDict sysDict,@AuthenticationPrincipal SysUserDetails sysUserDetails) throws SysException {
        sysDict.setCreatorid(sysUserDetails.getId());
        sysDict.setCreatorname(sysUserDetails.getUsername());
        sysDictService.updateOrSaveDict(sysDict);
        return Res.succ(SysUtils.sysDictList);
    }

    /**
     * 根据id返回数据字典
     * @param id
     * @return
     */
    @RequestMapping("/dict/getById")
    public Res<ArrayList<SysDict>> getById(@RequestParam("id") String id) {
        return Res.succ(SysUtils.sysDictMap.get(id));
    }

}
